package com.service;

import com.bean.User;
import com.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author hecn
 * @version 1.0
 * @date 2020/8/1 0001 23:14
 * @company
 */
//@WebServlet(name = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String name = req.getParameter("name");
        String password = req.getParameter("password");

        UserDao userDao = new UserDao();
        User user = userDao.login(name, password);

        if (user != null) {
            req.getRequestDispatcher("/WEB-INF/success.jsp").forward(req, resp);
        } else {
            resp.sendRedirect("register.jsp");
        }
    }
}
